package com.unittec.zk.provider.work.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unittec.zk.provider.scope.ThreadLocalScope;
import com.unittec.zk.provider.utils.CopyUtils;
import com.unittec.zk.sdk.root.internal.exception.BusinessException;
import com.unittec.zk.sdk.work.request.Req202004;
import com.unittec.zk.sdk.work.request.Req202005;
import com.unittec.zk.sdk.work.request.Req202006;
import com.unittec.zk.sdk.work.request.Req202007;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unittec.zk.provider.work.entity.CarApplication;
import com.unittec.zk.provider.work.mapper.CarApplicationMapper;

@Service
public class CarApplicationService extends ServiceImpl<CarApplicationMapper, CarApplication> {

    /**
     * 用车申请-增/改
     *
     * @param req202004
     * @return
     */
    public Object ApplicationSaveOrUpdate(Req202004 req202004) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String now = df.format(new Date(System.currentTimeMillis()));
        if (df.format(req202004.getStartTime()).compareTo(now) < 0 || df.format(req202004.getEndTime()).compareTo(now) < 0) {
            throw new BusinessException("请正确选择预约时间");
        }
        CarApplication carApplication = new CarApplication();
        CopyUtils.copyProperties(req202004, carApplication);
        //默认申请直接成功
        carApplication.setApplicationState(2);

        this.saveOrUpdate(carApplication);
        return "Success";
    }

    /**
     * 人员选择
     *
     * @param req202005
     * @return
     */
    public Object selectUser(Req202005 req202005) {
        String realname = req202005.getName();
        if (StrUtil.isNotBlank(req202005.getName())) {
            realname = "%" + realname + "%";
        }
        List<Map<String, Object>> list = this.baseMapper.selectUser(realname);
        return list;
    }

    /**
     * 用车申请-分页
     *
     * @param req202006
     * @return
     */
    public Object getApplicationPage(Req202006 req202006) {
        LambdaQueryWrapper<CarApplication> qw = new LambdaQueryWrapper<>();
        //去掉逻辑删除
        qw.apply("t1.del_flag=0");

        //只会显示用车人或申请人为本人的记录
        String userId = ThreadLocalScope.getAuthData().getUser();
        qw.apply("(t1.user_id = '"+userId+"' OR t1.create_user = '"+userId+"')");

        //用车人
        if (StrUtil.isNotBlank(req202006.getRealname())) {
            qw.apply("t3.realname LIKE '%"+req202006.getRealname()+"%'");
        }

        //车牌号
        if (StrUtil.isNotBlank(req202006.getTruckNumber())) {
            qw.apply("t2.truck_number LIKE '%"+req202006.getTruckNumber()+"%'");
        }

        //状态
        if (req202006.getApplicationState() != null) {
            qw.apply("t1.application_state = "+req202006.getApplicationState());
        }

        IPage<Map<String, Object>> page = this.baseMapper.getApplicationPage(new Page<>(req202006.getCurrent(), req202006.getSize()), qw);
        return page;
    }

    /**
     * 用车申请-逻辑删除
     * @param req202007
     * @return
     */
    public Object delApplicationByIds(Req202007 req202007) {
        this.removeByIds(req202007.getIds());
        return "Success";
    }
}
